//
//  TraderecordVC.m
//  代理端
//
//  Created by sliver on 15/12/12.
//  Copyright (c) 2015年 sliver. All rights reserved.
//

#import "TraderecordVC.h"
#import "Unit.h"
#import "PullTableView.h"
#import "WXDataService.h"
//#import "TraderModel.h"
#import "TraderTableViewCell.h"
@interface TraderecordVC ()<UITableViewDataSource,UITableViewDelegate,PullTableViewDelegate>
{
    PullTableView *tableview;
    UILabel *nameLab; //商品名称
    UILabel *modelLab; //商品型号
    UILabel *numLab; //数量
    UILabel *moneyLab; //交易金额
    UILabel *timeLab; // 时间

    NSMutableArray *dataAry;
    int page;
}
@end

@implementation TraderecordVC


- (void)viewDidLoad {
    [super viewDidLoad];
    dataAry = [[NSMutableArray alloc]init];
    page = 1;
    [self loadView1];
    [self loadNav];
    [self loadDatawithPage:@"1"];
    // Do any additional setup after loading the view.
}
-(void)loadDatawithPage:(NSString *)pages{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:pages forKey:@"page"];
    [params setObject:USER_ACCOUNT forKey:@"agency_id_"];
    [WXDataService requestWithURL:@"mobile/agency/listDealPage.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        NSLog(@"%@",result);
        [dataAry removeAllObjects];

        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            NSArray *arytemp = result[@"agency_dealPOList"][@"listItems"];
            for (NSDictionary *dic in arytemp) {
                
                TraderModel *model = [[TraderModel alloc]initWithDataDic:dic];
                
                
                [dataAry addObject:model];
            }
            if (dataAry.count == 0) {
                UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:@"无更多库存" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [deleAlertView show];
                return;
            }
            [tableview reloadData];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
-(void)loadDatawithPage2:(NSString *)pages{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:pages forKey:@"page"];
    [params setObject:USER_ACCOUNT forKey:@"agency_id_"];
    [WXDataService requestWithURL:@"mobile/agency/listDealPage.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            NSArray *arytemp = result[@"agency_dealPOList"][@"listItems"];
            for (NSDictionary *dic in arytemp) {
                
                TraderModel *model = [[TraderModel alloc]initWithDataDic:dic];
                
                
                [dataAry addObject:model];
            }
            if (dataAry.count == 0) {
                UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:@"无更多库存" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [deleAlertView show];
                return;
            }
            [tableview reloadData];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

#pragma mark - nav
-(void)loadNav{
    self.view.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 20)];
    redview.backgroundColor = DRINK_RED_COLOR;
    [self.view addSubview:redview];
    
    UILabel *redLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenW, 40)];
    redLab.backgroundColor = DRINK_RED_COLOR;
    redLab.text = @"交易记录";
    redLab.textColor = [UIColor whiteColor];
    redLab.textAlignment =  NSTextAlignmentCenter;
    redLab.font = [UIFont boldSystemFontOfSize:19];
    [self.view addSubview:redLab];
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20,44, 44)];
    backBtn.adjustsImageWhenHighlighted = NO;
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
}
-(void)loadView1{
    tableview = [[PullTableView alloc]initWithFrame:CGRectMake(0, 60, ScreenW, ScreenH - 60) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.pullDelegate = self;
    tableview.tableFooterView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self.view addSubview:tableview];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     NSString *cellIdStr = @"traderCell";
    
    UINib *nib = [UINib nibWithNibName:@"TraderTableViewCell" bundle:nil];
    
    [tableView registerNib:nib forCellReuseIdentifier:cellIdStr];
    
    TraderTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:cellIdStr];
    
    [cell gaveMeModel:dataAry[indexPath.row]];
    return cell;
    
//    if(!cell){
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdStr];
//        nameLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 150, 20)];
//        nameLab.font = [UIFont boldSystemFontOfSize:17];
//        nameLab.textColor = [UIColor blackColor];
//        [cell.contentView addSubview:nameLab];
//        
//        modelLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 110, 20)];
//        modelLab.font = [UIFont systemFontOfSize:14];
//        modelLab.textColor =[UIColor grayColor];
//        [cell.contentView addSubview:modelLab];
//        
//        numLab = [[UILabel alloc]initWithFrame:CGRectMake(120, 30, 100, 20)];
//        numLab.font = [UIFont boldSystemFontOfSize:13];
//        numLab.textColor = [UIColor blackColor];
//        [cell.contentView addSubview:numLab];
//        
//        timeLab = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW - 70, 5, 70, 20)];
//        timeLab.font = [UIFont systemFontOfSize:12];
//        timeLab.textColor = [UIColor grayColor];
//        [cell.contentView addSubview:timeLab];
//        
//        moneyLab = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW -70, 30, 70, 20)];
//        moneyLab.font = [UIFont boldSystemFontOfSize:22];
//        moneyLab.textColor = [UIColor blackColor];
//        [cell.contentView addSubview:moneyLab];
//        
//    
//    nameLab.text = [dataAry objectAtIndex:indexPath.row][@"file1"];
//    modelLab.text =  [dataAry objectAtIndex:indexPath.row][@"file2"];
//    numLab.text =  [NSString stringWithFormat:@"x%@",[dataAry objectAtIndex:indexPath.row][@"file3"]];
//    NSArray *timeAry = [[dataAry objectAtIndex:indexPath.row][@"tmstamp"] componentsSeparatedByString:@" "];
//    timeLab.text =timeAry[0];
//    moneyLab.text =[NSString stringWithFormat:@"%@",[dataAry objectAtIndex:indexPath.row][@"money"]];
//        if ([dataAry[indexPath.row][@"file1"] isEqualToString:@"充值"] || [dataAry[indexPath.row][@"file1"] isEqualToString:@"返利"]) {
//            numLab.text = @"";
//            modelLab.text = @"";
//              moneyLab.text =[NSString stringWithFormat:@"+%@",[dataAry objectAtIndex:indexPath.row][@"money"]];
//        }
//    }
//    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
//    return cell;
}
//设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TraderTableViewCellHeight;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataAry.count;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

-(void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView{
    page++;
    [self performSelector:@selector(refreshTable) withObject:nil afterDelay:2];
    [self loadDatawithPage2:[NSString stringWithFormat:@"%i",page]];
    
}
- (void) refreshTable
{
    tableview.pullTableIsLoadingMore = NO;
    tableview.pullTableIsRefreshing = NO;
}

-(void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView{
    [self performSelector:@selector(refreshTable) withObject:nil afterDelay:2];
    page =1;
    [self loadDatawithPage:@"1"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
